extern crate fejpsl_contract;
use fejpsl_contract::exam::multiple_answer::MultipleAnswer;

pub async fn get_multiple_answer_by_id(id:&i32) -> Option<MultipleAnswer> {
    let client = super::super::db::connect().await.unwrap();
    let rows = &client.query("SELECT a.id, score, correct, info_text_content_id,b.content
	    FROM public.t_question a left join t_text_content b on a.info_text_content_id=b.id
	    where question_type=2 and a.id=$1", &[&id]).await.unwrap();
    let row = rows.get(0).unwrap();
    let multiple_answer = MultipleAnswer {
        id:   row.get(0),
        score: row.get(1),
        correct:  row.get(2),
        info_text_content_id:   row.get(3),
        content: row.get(4),
    };
    return Some(multiple_answer);
}

pub async fn list_multiple_answer(subject_id:&i32) -> Option<Vec<MultipleAnswer>> {
    let client =super::super::db::connect().await.unwrap();
    let mut vec_multiple_answer = Vec::new();
    let rows = &client.query("SELECT a.id, score, correct, info_text_content_id,b.content
	    FROM public.t_question a left join t_text_content b on a.info_text_content_id=b.id
	    where question_type=2 and subject_id=$1;", &[&subject_id]).await.unwrap();
    for row in rows {
        let multiple_answer = MultipleAnswer {
            id:   row.get(0),
            score: row.get(1),
            correct:  row.get(2),
            info_text_content_id:   row.get(3),
            content: row.get(4),
        };
        vec_multiple_answer.push(multiple_answer);
    }
    return Some(vec_multiple_answer);
}
